package com.elh.lock;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

/**
 * @author liulei
 **/
public class EasyExcelTest {

    static List<Bean> list = new ArrayList<Bean>();

    static Map<String,String> map = new HashMap<>();

    static {
        map.put("2015110300675925","111");
        map.put("263","263");
        map.put("2016042801344859","143");
        map.put("2017042406926788","143");
        map.put("2017052407332049","100001950");


        map.put("2017101209261251","138");
        map.put("2018020702156623","100015578");
        map.put("2018112262294133","100000816");
        map.put("2019070265715446","273");
        map.put("2019071065846034","104");
        map.put("2019091667407874","262");
        map.put("2021002112694330","109");
    }


    public static void main(String[] args) throws Exception {
        String sql = "UPDATE hub_pay_info SET pay_state = '1',web_pay_no = '{}' where out_trade_no = '{}' and unit_id ='{}';";
        String  fileName =  System.currentTimeMillis() + ".xlsx";
        BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\liulei-space\\elh-parent\\elh-component\\elh-lock\\src\\main\\resources\\test22.log"));
        String line = null;
        while((line = bufferedReader.readLine()) != null){
            line = line.trim();
            String app_id = map.get(StrUtil.subBetween(line, "app_id=", "&"));
            Objects.requireNonNull(app_id);
            String out_trade_no = StrUtil.subBetween(line, "out_trade_no=", "&");
            String total_amount = StrUtil.subBetween(line, "total_amount=", "&");
            line = StrUtil.removeAll(line,"out_trade_no=");
            String trade_no = StrUtil.subBetween(line, "trade_no=", "&");
            Bean bean = new Bean();
            bean.setApp_id(app_id);
            bean.setOut_trade_no(out_trade_no);
            bean.setAmount(total_amount);
            bean.setWeb_pay_no(trade_no);
            bean.setSql(StrUtil.format(sql,trade_no,out_trade_no,app_id));
            if(list.contains(bean)){
                continue;
            }
            list.add(bean);
        }
        System.out.println(fileName);
        EasyExcel.write(fileName, Bean.class).sheet("data").doWrite(list);
        IoUtil.close(bufferedReader);
    }




    @Data
    private static class Bean {
        @ExcelProperty("app_id")
        private String app_id;

        @ExcelProperty("amount")
        private String amount;

        @ExcelProperty("web_pay_no")
        private String web_pay_no;

        @ExcelProperty("out_trade_no")
        private String out_trade_no;

        @ExcelProperty("sql")
        private String sql;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Bean bean = (Bean) o;
            return Objects.equals(app_id, bean.app_id) &&
                    Objects.equals(web_pay_no, bean.web_pay_no) &&
                    Objects.equals(out_trade_no, bean.out_trade_no);
        }

        @Override
        public int hashCode() {
            return Objects.hash(app_id, web_pay_no, out_trade_no);
        }
    }

}
